package com.qiange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiange.common.BaseContext;
import com.qiange.common.Result;
import com.qiange.pojo.ShoppingCart;
import com.qiange.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 展示该用户购物车的全部菜品
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {

        Long userid = BaseContext.getId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userid);

        //查询该用户的购物车中的商品
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        return Result.success(shoppingCartList);
    }

    /**
     * 添加购物车
     *
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
//        Long userid = (Long) session.getAttribute("user");

        Long userId = (Long) redisTemplate.opsForValue().get("user");

        shoppingCart.setUserId(userId);

        //判断该用户购物车中是否有该菜品或者套餐
        //判断本次添加的商品为菜品还是套餐，再去判断购物车中是否有该商品

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);

        if (shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lqw);

        //如果存在，则数量直接加一,然后返回加过数量的实体，回显用于页面商品数量修改回显
        //如果不存在，直接添加到数据库，数量字段默认为1，直接返回该实体类用于页面商品数量回显
        if (shoppingCartOne == null) {

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartService.save(shoppingCart);

            return Result.success(shoppingCart);

        } else {
            Integer number = shoppingCartOne.getNumber();

            shoppingCartOne.setNumber(number + 1);

            shoppingCartService.updateById(shoppingCartOne);

            return Result.success(shoppingCartOne);

        }
    }

    /**
     * 减少购物车选择商品的数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> reduceNum(@RequestBody ShoppingCart shoppingCart) {

        Long userId = BaseContext.getId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        lqw.eq(ShoppingCart::getUserId,userId);

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lqw);
        Integer number = shoppingCartOne.getNumber();

        if (number <= 1){
            shoppingCartService.remove(lqw);
            return Result.success(shoppingCart);
        }else {
            shoppingCartOne.setNumber(number - 1);
            shoppingCartService.updateById(shoppingCartOne);
            return Result.success(shoppingCartOne);
        }

    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> cleanShoppingCart(){

        Long userId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);

        shoppingCartService.remove(lqw);

        return Result.success("清空购物车成功");
    }
}
